/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Jan 8, 2012, 11:20:25 PM
 */
package GUI;

import Bussiness.HangHoa;
import Bussiness.PhieuXuat;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Locale;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Admin
 */
public class jpXuatHang extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    static String bot;
    private DefaultTableModel modelTable = new DefaultTableModel();
    /** Creates new form NhapHang */
    public jpXuatHang() {
        initComponents();
        bot = "";
        this.display();
        
        loadMaHang();
    }
    public void loadMaHang(){
        try {
            cbmahang.removeAllItems();
            HangHoa ex=new HangHoa();
            Vector v=ex.getAllHangHoa();
            for(int i=0;i<v.size();i++){
                cbmahang.addItem(((HangHoa)v.get(i)).getMahang());
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    public void reset()
    {
        cbmahang.getSelectedIndex();
        txtslyc.setText("");
        txtsophieunhap.setText("");
        txtmakh.setText("");
        txtngayxuat.setText("");
    }
    public void loadTable(Vector v)
    {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã Hàng");
        v1.addElement("Số Phiếu");
        v1.addElement("Số Lượng Xuất");
        v1.addElement("Ngày Xuất");
        v1.addElement("Mã Khách Hàng");
        modelTable.setColumnIdentifiers(v1);
        PhieuXuat b = new PhieuXuat();
        for (int i = 0; i < v.size(); i++) {
            b = (PhieuXuat)v.get(i);
            Vector row = new Vector();
            
            row.addElement(b.getMaHang());
            row.addElement(b.getSoPhieu());
            row.addElement(b.getSlx());
            row.addElement(b.getNgayXuat());
            row.addElement(b.getMaKH());
            modelTable.addRow(row);
        }
        tblXuatHang.setModel(modelTable);
        int r = tblXuatHang.getSelectedRow();
        if(r < 0){
            this.bot = "";
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);
            return;
        }
        else{
            bot = tblXuatHang.getValueAt(r, 0).toString();
        }
    }
    public void display()
    {
        PhieuXuat b1 = new PhieuXuat();
        Vector v1 = new Vector();
        try {
           v1=b1.getAllPhieuX(); 
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v1);
    }
    public void add()
    {
        PhieuXuat b = new PhieuXuat();
        b.setMaHang((String)cbmahang.getSelectedItem());
        b.setSoPhieu(txtsophieunhap.getText());
        b.setSlx(Integer.parseInt(txtslyc.getText()));
        b.setNgayXuat(txtngayxuat.getText());
        b.setMaKH(txtmakh.getText());
        int t = 0;
        try{
            t = b.InsertPhieuX(b);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"Add successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Add erro!");
       this.reset();
    }
    public void update()
    {
        PhieuXuat b = new PhieuXuat();
        b.setMaHang((String)cbmahang.getSelectedItem());
        b.setSoPhieu(txtsophieunhap.getText());
        b.setSlx(Integer.parseInt(txtslyc.getText()));
        b.setNgayXuat(txtngayxuat.getText());
        b.setMaKH(txtmakh.getText());
        int t = 0;
        try{
            t = b.UpdatePhieuX(b);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"Update successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Update erro!");
       this.reset();
    }
    public void search(){
        PhieuXuat b=new PhieuXuat();
        Vector v=new Vector();        
        try {
                v=b.getPX_SoPhieu(txtSearch.getText());
        } catch (SQLException e) {
            e.printStackTrace();
        }  
          this.loadTable(v);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtsophieunhap = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        cbmahang = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtmakh = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtSearch = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        cboSearch = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblXuatHang = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        txtslyc = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtngayxuat = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();

        jButton6.setText("reset");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        cbmahang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Số phiếu");

        jButton5.setText("Tìm Kiếm");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton1.setText("Thêm");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel4.setText("Mã khách hàng");

        jLabel7.setText("Tìm theo: ");

        jLabel3.setText("Số lượng xuất");

        jButton4.setText("Load");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton3.setText("Xóa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel5.setText("Ngày xuất");

        cboSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Số Phiếu" }));

        tblXuatHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblXuatHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblXuatHangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblXuatHang);

        jButton2.setText("Sửa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Mã Hàng");

        jButton7.setText("In Hóa Đơn");

        jButton8.setText("Số Hóa Đơn Trong Ngày");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton7)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton8)
                        .addGap(233, 233, 233)
                        .addComponent(jLabel7)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton5))
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbmahang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtsophieunhap, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtslyc, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtmakh, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(38, 38, 38)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtngayxuat, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(41, 41, 41))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbmahang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtsophieunhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtslyc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtmakh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(txtngayxuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton6))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton8)
                    .addComponent(jLabel7))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton5))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jButton7)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    this.reset();
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    this.add();
    this.display();
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
// TODO add your handling code here:
    this.display();
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
     if(!bot.equals("")){
        int show=JOptionPane.showConfirmDialog(this,"Are You sure delete ?","Inform",JOptionPane.YES_NO_OPTION);
        int t=0,count=0;
        PhieuXuat b=new PhieuXuat();
        if(show==JOptionPane.YES_OPTION){
            int [] rArr=tblXuatHang.getSelectedRows();
                try 
                {
                    for(int i=0;i<rArr.length;i++)
                    {
                        t=b.DelPhieuXuat(tblXuatHang.getValueAt(rArr[i],1).toString());
                        if(t==1)
                            count++;
                    }
                    if(count>0)
                    {
                        JOptionPane.showMessageDialog(this,"Deleted "+count+" record ","successful.",JOptionPane.INFORMATION_MESSAGE);
                        this.display();
                    }
                    else
                    {
                      JOptionPane.showMessageDialog(this,"Error","Inform",JOptionPane.ERROR_MESSAGE);  
                    }
                } catch (SQLException x) {               
                   System.out.println(x);
                }
        }
    }
    this.reset();
}//GEN-LAST:event_jButton3ActionPerformed

private void tblXuatHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblXuatHangMouseClicked
// TODO add your handling code here:
    int r = tblXuatHang.getSelectedRow();
    if(r < 0){
        bot = "";
        return;
    }else{
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);
        bot = tblXuatHang.getValueAt(r, 0).toString();
        
        if (r != -1){
            txtsophieunhap.setText(String.valueOf(tblXuatHang.getValueAt(r, 1)));
            cbmahang.setSelectedItem(String.valueOf(tblXuatHang.getValueAt(r, 0)));
            txtngayxuat.setText(String.valueOf(tblXuatHang.getValueAt(r, 3)));
            txtslyc.setText(String.valueOf(tblXuatHang.getValueAt(r, 2)));
            txtmakh.setText(String.valueOf(tblXuatHang.getValueAt(r, 4)));
        }
    }
}//GEN-LAST:event_tblXuatHangMouseClicked

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    this.update();
    this.display();
}//GEN-LAST:event_jButton2ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
// TODO add your handling code here:
    this.search();
}//GEN-LAST:event_jButton5ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbmahang;
    private javax.swing.JComboBox cboSearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblXuatHang;
    private javax.swing.JTextField txtSearch;
    private javax.swing.JTextField txtmakh;
    private javax.swing.JTextField txtngayxuat;
    private javax.swing.JTextField txtslyc;
    private javax.swing.JTextField txtsophieunhap;
    // End of variables declaration//GEN-END:variables
}
